import os

import django
from django.db.models import Count



if __name__ =='__main__':
    # django 代码的运行要在一个特定的环境中
    # django flask本质上都是提供了一个符合WSGI规范的函数
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myblogproject.settings')
    django.setup()
    from myblog.models import Category
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myblogproject.settings')
    categories=Category.objects.annotate(
        num_articles = Count('article')).filter(num_articles__gt=0)
    print(categories)
    for category in categories:
        print(category,category.num_articles )

